DECLARE @LANCOD BIGINT, @VALOR MONEY, @MSG VARCHAR(8000)

DECLARE @LISTA CURSOR

SET @LISTA = CURSOR FOR 
	SELECT 
		CAST ( SUBSTRING(BXI_IDENTIFICADOR, 5, 11) AS BIGINT)
		, COALESCE ( BXI_VALOR_TITULO, 0 )
	FROM  TRB_LAURO_BAIXA_BANCARIA_ITEM
	WHERE BXI_CONVENIO           = '102409'
	AND   BXI_BANCO_CODIGO       = '104'
	AND   BXI_SEQUENCIAL_ARQUIVO IN ( '001540', '001541')

OPEN @LISTA

FETCH NEXT FROM @LISTA INTO @LANCOD, @VALOR

WHILE @@FETCH_STATUS = 0
BEGIN 
	IF  EXISTS ( SELECT 1 FROM TRB_LANCAMENTO WHERE LAN_COD = @LANCOD )
		AND NOT EXISTS ( SELECT 1 FROM TRB_BAIXA WHERE LAN_COD = @LANCOD )
	BEGIN
		SET @MSG = CONVERT( SYSNAME, @LANCOD )	
		PRINT @MSG
	END
	FETCH NEXT FROM @LISTA INTO @LANCOD, @VALOR
END
]
CLOSE @LISTA
DEALLOCATE @LISTA